<!DOCTYPE html>
<html>

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0,minimum-scale=1.0,maximum-scale=1.0" />
  <meta name="author" content="火星科技 http://mars3d.cn ">
  <meta name="apple-touch-fullscreen" content="yes">
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="apple-mobile-web-app-status-bar-style" content="black">
  <meta name="format-detection" content="telephone=no">
  <meta name="x5-fullscreen" content="true">
  <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1" />
  <!-- 标题及搜索关键字 -->
  <meta name="keywords" content="火星科技,cesium,3D,GIS,marsgis,三维,地球,地图,开发,框架,系统,示例,资料,模型,离线,外包,合肥,安徽,中国" />
  <meta name="description"
        content="火星科技 合肥火星 合肥火星科技 合肥火星科技有限公司 leaflet leaflet框架 leaflet开发 cesium cesium开发 cesium框架 三维 地球 模型  gis marsgis 地图离线 地图开发 地图框架 地图外包 框架 开发 外包  地图离线 二维地图 三维地图 全景漫游 地理信息系统 云GIS 三维GIS GIS平台 WebGIS" />

  <link rel="shortcut icon" type="image/x-icon" href="http://mars3d.cn/favicon.ico">
  <title>路径规划 | Mars3D | 三维地图 | 火星科技 | 合肥火星科技有限公司</title>


  <!--第三方lib-->
  <script type="text/javascript" src="../lib/include-lib.js" libpath="../lib/"
    include="jquery,font-awesome,bootstrap,layer,haoutil,toastr,turf,mars3d"></script>

  <link href="css/style.css" rel="stylesheet" />
</head>

<body class="dark">
  <!--加载前进行操作提示，优化用户体验-->
  <div id="mask" class="signmask" onclick="removeMask()"></div>

  <div id="mars3dContainer" class="mars3d-container"></div>

  <div class="infoview">
    <table class="mars-table">
      <tr>
        <td>
          方式
        </td>
        <td colspan="2">
          <select id="queryType" class="selectpicker form-control">
            <option value="1" selected="selected">步行路线查询</option>
            <!-- <option value="2">骑行路线查询</option> -->
            <option value="3">驾车路线查询</option>
          </select>
        </td>
      </tr>
      <tr>
        <td>
          起点
        </td>
        <td>
          <input id="startIpt" type="text" value="" class="form-control" style="width: 200px;" />
        </td>
        <td>
          <input type="button" class="btn btn-primary" value="选点" id="startPoint" />
        </td>
      </tr>
      <tr>
        <td>
          终点：
        </td>
        <td>
          <input id="endIpt" type="text" value="" class="form-control " style="width: 200px;" />
        </td>
        <td>
          <input type="button" class="btn btn-primary" value="选点" id="endPoint" />
        </td>
      </tr>
      <tr>
        <td>
        </td>
        <td colspan="3">
          <input type="button" class="btn btn-primary" value="开始分析" id="FX" />
          <input type="button" class="btn btn-primary" value="清除" id="clear" />
        </td>
      </tr>
    </table>
  </div>

  <div class="infoview result" style="top:250px;display: none;word-break:break-all; width: 300px;">
    总距离：<span id="allDiatance" style="color: #cad1d1;"></span><br />
    预计时间：<span id="useTime" style="color: #cad1d1;"></span><br />
    导航：<span id="dh" style="color: #cad1d1;"></span><br />
  </div>


  <script src="./js/common.js"></script>
  <script type="text/javascript">
    'use script' //开发环境建议开启严格模式

    var map
    var routeLayer
    var gaodeRoute

    //当前页面业务相关
    var startGraphic, endGraphic

    var lineGraphic

    function initMap(options) {
      //合并属性参数，可覆盖config.json中的对应配置
      var mapOptions = mars3d.Util.merge(options, {
        scene: {
          center: {
            y: 31.797919,
            x: 117.281329,
            z: 36235.64,
            heading: 357.9,
            pitch: -81.4,
            roll: 0,
          },
        },
      })

      //创建三维地球场景
      map = new mars3d.Map('mars3dContainer', mapOptions)

      //创建矢量数据图层
      routeLayer = new mars3d.layer.GraphicLayer()
      map.addLayer(routeLayer)

      gaodeRoute = new mars3d.query.GaodeRoute({
        // key: ['ae29a37307840c7ae4a785ac905927e0'],
      })

      $('#startPoint').click(function () {
        if (startGraphic) {
          startGraphic.remove()
          startGraphic = null
        }

        map.graphicLayer.startDraw({
          type: 'billboard',
          style: {
            image: 'img/marker/start.png',
            horizontalOrigin: Cesium.HorizontalOrigin.CENTER,
            verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
          },
          success: function (graphic) {
            startGraphic = graphic

            var point = graphic.point
            point.format()
            $('#startIpt').val(point.lng + ',' + point.lat)

            queryRoute()
          },
        })
      })
      $('#endPoint').click(function () {
        if (endGraphic) {
          endGraphic.remove()
          endGraphic = null
        }

        map.graphicLayer.startDraw({
          type: 'billboard',
          style: {
            image: 'img/marker/end.png',
            horizontalOrigin: Cesium.HorizontalOrigin.CENTER,
            verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
          },
          success: function (graphic) {
            endGraphic = graphic

            var point = graphic.point
            point.format()
            $('#endIpt').val(point.lng + ',' + point.lat)

            queryRoute()
          },
        })
      })

      //开始分析
      $('#FX').click(function () {
        if (!startGraphic || !endGraphic) {
          haoutil.msg('请设置起点和终点')
          return
        }
        queryRoute()
      })

      $('#clear').click(function () {
        if (startGraphic) {
          startGraphic.remove()
          startGraphic = null
        }
        if (endGraphic) {
          endGraphic.remove()
          endGraphic = null
        }
        $('#startIpt').val('')
        $('#endIpt').val('')

        routeLayer.clear()
        $('.result').hide()
      })
    }

    function queryRoute() {
      if (!startGraphic || !endGraphic) {
        return
      }

      routeLayer.clear()

      haoutil.loading.show()
      $('.result').hide()

      var type = Number($('#queryType').val()) //GaodeRouteType枚举类型

      gaodeRoute.query({
        type: type,
        points: [startGraphic.coordinate, endGraphic.coordinate],
        success: function (data) {
          haoutil.loading.close()

          let lineFirst = data.paths[0]
          var points = lineFirst.points
          if (!points || points.length < 1) {
            return
          }

          var time = haoutil.str.formatTime(lineFirst.allDuration)
          var distance = haoutil.str.formatLength(lineFirst.allDistance)
          var html = '<div>总距离：' + distance + '<br/>所需时间：' + time + '</div>'

          let graphic = new mars3d.graphic.PolylineEntity({
            positions: points,
            style: {
              clampToGround: true,
              material: Cesium.Color.AQUA.withAlpha(0.8),
              width: 5,
            },
            popup: html,
          })
          routeLayer.addGraphic(graphic)

          showResult(data)
        },
        error: function (msg) {
          haoutil.loading.close()

          window.toastr.error(msg)
        },
      })
    }

    function showResult(data) {
      $('.result').show()

      var allTime = haoutil.str.formatTime(data.paths[0].allDuration)
      var allDistance = haoutil.str.formatLength(data.paths[0].allDistance)

      var dhHtml = ''
      for (var i = 0; i < data.paths[0].steps.length; i++) {
        var item = data.paths[0].steps[i]
        dhHtml += item.instruction + '；'
      }
      $('#useTime').html(allTime)

      $('#allDiatance').html(allDistance)
      $('#dh').html(dhHtml)
    }
  </script>
</body>

</html>
